const WXAPI = require('apifm-wxapi')
Page({


    data: {
        shop:{},
        page: 1,
        pageSize: 10,
        hasMore:true,
        list: []
    },


    onLoad(options) {
       
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
      this.setData({
        list:[],
        page:1,
        hasMore:true,
      })
      this.initShop()
      this.initData()
    },

    async initData() {
      wx.showLoading({
        title: '',
      })
        const page = this.data.page
        const pageSize = this.data.pageSize
        const res = await WXAPI.deliveryFreeAddresses({page, pageSize})
        wx.hideLoading()
        const list = this.data.list
        list.push(...res.data)
        let hasMore = true
       if(res.data.length === 0){
        hasMore = false
       }
        this.setData({list,hasMore})
    },

    selectAddress(e) {
      const item = e.currentTarget.dataset.item
        let pages = getCurrentPages();

        //上一页面
        let prevPage = pages[pages.length - 2];

        //将数值信息赋值给上一页面cityItem变量
        prevPage.setData({
            freeAddress: item,
            chooseFrom:"free"
        });
      wx.navigateBack()
    },
    openAddress(e) {
        const {longitude, latitude} = e.currentTarget.dataset.item
        wx.openLocation({
          longitude,
          latitude,
          fail:()=>{
          wx.showToast({
            title: '打开失败',
            icon:"error"
          })
        }})

    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
      this.setData({
        list:[],
        page:1,
        hasMore:true,
      })
      this.initData()
      wx.stopPullDownRefresh()
    },

    initShop(){
      const shop = wx.getStorageSync('shopInfo')
      this.setData({shop})
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
      if(!this.data.hasMore){
        return ;
      }
     
      let page = this.data.page
      page ++
      this.setData({page})
        this.initData()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})